/**
 * Author: OMAROMAN
 * Date: 10/20/11
 * Time: 12:47 PM
 */

package util.play;

import play.mvc.Http;

import java.util.Map;

public class RequestUtility {

    public static String getScheme() {
        Http.Request request = Http.Request.current();
        return request.getBase().substring(0, request.getBase().indexOf(':'));
    }

    public static String getBase() {
        Http.Request request = Http.Request.current();
        return String.format("%s%s", request.domain, (request.port == 80 || request.port == 443) ? "" : ":" + request.port);
    }

    public static boolean isIE() {
        Http.Request request = Http.Request.current();
        Http.Header uaHeader = request.headers.get("user-agent");
        return uaHeader != null && uaHeader.value().contains("MSIE");
    }

    public static String getHeaderIgnoreCase(String headerName) {
        Http.Request request = Http.Request.current();
        Map<String, Http.Header> headers = request.headers;
        for (String name : headers.keySet()) {
            if (headerName.equalsIgnoreCase(name)) {
                return headers.get(name).value();
            }
        }
        return null;
    }
}

